package com.two.screen.water.balance.ui.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.KeyEvent
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.jakewharton.rxbinding2.view.RxView
import com.two.screen.water.balance.R
import com.two.screen.water.balance.image.ImageManger
import com.two.screen.water.balance.net.api.DataApi
import com.two.screen.water.balance.ui.activity.base.BaseActivity
import com.two.screen.water.balance.utils.ARouterUtils
import com.two.screen.water.balance.utils.eventbus.Event
import com.two.screen.water.balance.utils.eventbus.EventCode
import com.two.screen.water.balance.utils.eventbus.EventCode.Companion.HANG_GO_COLLECT
import kotlinx.android.synthetic.main.activity_function_menu24.*
import kotlinx.android.synthetic.main.activity_function_menu24.mGoBackLayout
import kotlinx.android.synthetic.main.activity_function_menu24.mSystemSettingLayout
import java.util.concurrent.TimeUnit


@Route(path = ARouterUtils.FunctionMenuActivity24)
class FunctionMenuActivity24 : BaseActivity() {
    val TAG = FunctionMenuActivity24::class.java.simpleName.plus("=>")
    override fun getContentLayout(): Any = R.layout.activity_function_menu24


    override fun initView(savedInstanceState: Bundle?) {
        ARouter.getInstance().inject(this)
    }

    override fun initData() {

        val seller = DataApi.mSellerInfo?.merchantList?.firstOrNull()
        ImageManger.setCircleImg(this, seller?.personName, mUserLogoIv)

        mUserNameTv.text = seller?.personName?.plus("(${seller.personPhone})")
        mLoginTimeTv.text = DataApi.mSellerInfo?.loginTime
    }


    @SuppressLint("CheckResult")
    override fun initListener() {
        mSystemSettingLayout.setOnClickListener {
            ARouter.getInstance()
                .build(ARouterUtils.SystemSettingActivity24)
                .navigation()
        }
        mOrderQueryLayout.setOnClickListener {
            ARouter.getInstance()
                .build(ARouterUtils.OrderServerActivity24)
                .navigation()
        }

        RxView.clicks(mGoBackLayout)
            .throttleFirst(1, TimeUnit.SECONDS)
            .subscribe { finish() }
    }

    override fun isRegisterEventBus(): Boolean =true
    override fun receiveEvent(event: Event<Any>) {
        super.receiveEvent(event)
        when (event.code) {
            EventCode.FINISH_FUNCTION_MENU_ACTIVITY,HANG_GO_COLLECT ->  mGoBackLayout.performClick()
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (event?.device?.name?.contains("USB", true) != true) return false
        if (event.action == KeyEvent.ACTION_DOWN) {//处理触发两次
            when (keyCode) {
                33 -> { //d("帐户/E")
                    mGoBackLayout.performClick()
                    return true
                }
            }
        }
        return super.onKeyDown(keyCode, event)
    }
}